package net.bwie.flink;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;


/**
 * 调用HBase Java API，向表中写入数据
 * @author xuanyu
 * @date 2025/10/30
 */
public class _01HbasePutDemo {

	public static void main(String[] args) throws Exception{
		// 1. 连接
		/*
			文档：
				https://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/package-summary.html
			虚拟机node101或node102或node103，配置文件查看：
				cd /opt/module/hbase/conf
				more hbase-site.xml
		 */
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", "node101,node102,node103");
		Connection connection = ConnectionFactory.createConnection(conf);
		// 2. 表
		Table table = connection.getTable(TableName.valueOf("student"));
		// 3. 插入
		// put 'student', '1003', 'info:name', 'wangwu'
		Put put = new Put(Bytes.toBytes("1003")) ;
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("wangwu")) ;
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("23")) ;
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("address"), Bytes.toBytes("tianjin")) ;
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("gender"), Bytes.toBytes("female")) ;
		// 4. 执行
		table.put(put);
		// 5. 关闭
		table.close();
		connection.close();
	}

}
